package com.hmy.finance.service.core.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmy.finance.api.bo.core.request.enums.FeeTypeEnum;
import com.hmy.finance.common.enums.ProfitLossStatusEnum;
import com.hmy.finance.dal.mapper.DeviceProfitLossDetailMapper;
import com.hmy.finance.dal.po.DeviceProfitLossDetailPo;
import com.hmy.finance.service.core.IDeviceProfitLossDetailService;
import com.hmy.finance.service.core.dto.DeviceProfitLossDetailListQueryDto;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Objects;

/**
 * @author songkf
 * @description 针对表【device_profit_loss_detail(租赁设备损益费用明细表)】的数据库操作Service实现
 * @createDate 2024-02-21 18:55:01
 */
@Service
public class DeviceProfitLossDetailServiceImpl extends ServiceImpl<DeviceProfitLossDetailMapper, DeviceProfitLossDetailPo> implements IDeviceProfitLossDetailService {

    @Override
    public List<DeviceProfitLossDetailPo> queryTodayDeviceProfitLossDetailByDeviceId(Long deviceId, LocalDate handleDate) {

        /*
        这里需要查询属于当前租赁记录的，在处理日期之前的所有的未生效的设备损益明细记录
         */
        // 将 LocalDate 转换为 LocalDateTime，时间设为当天的结束时间（23:59:59）
        LocalDateTime endOfDay = handleDate.atTime(LocalTime.MAX);
        LambdaQueryWrapper<DeviceProfitLossDetailPo> queryWrapper = Wrappers.<DeviceProfitLossDetailPo>lambdaQuery()
                .eq(DeviceProfitLossDetailPo::getStatus, ProfitLossStatusEnum.WAIT_EFFECTIVE)
                .eq(DeviceProfitLossDetailPo::getDeviceRentalRecordId, deviceId).le(DeviceProfitLossDetailPo::getFeeIncurredTime, endOfDay);

        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public boolean checkInitialDayFeeWaiver(Long id) {
        return !this.baseMapper.selectList(Wrappers.lambdaQuery(DeviceProfitLossDetailPo.class)
                .eq(DeviceProfitLossDetailPo::getDeviceRentalRecordId, id)
                .eq(DeviceProfitLossDetailPo::getFeeType, FeeTypeEnum.INITIAL_DAY_FEE_WAIVER)
                .eq(DeviceProfitLossDetailPo::getStatus, ProfitLossStatusEnum.ALREADY_EFFECTIVE)
                .eq(DeviceProfitLossDetailPo::getIsDeleted, false)
        ).isEmpty();
    }

    @Override
    public List<DeviceProfitLossDetailPo> selectDeviceProfitLossDetailList(DeviceProfitLossDetailListQueryDto queryDto) {
        LambdaQueryWrapper<DeviceProfitLossDetailPo> queryWrapper = Wrappers.<DeviceProfitLossDetailPo>lambdaQuery();
        if (Objects.nonNull(queryDto.getContractId())) {
            queryWrapper.eq(DeviceProfitLossDetailPo::getContractId, queryDto.getContractId());
        }
        if (Objects.nonNull(queryDto.getDeviceRentalRecordId())) {
            queryWrapper.eq(DeviceProfitLossDetailPo::getDeviceRentalRecordId, queryDto.getDeviceRentalRecordId());
        }
        if (Objects.nonNull(queryDto.getFeeType())) {
            queryWrapper.eq(DeviceProfitLossDetailPo::getFeeType, queryDto.getFeeType());
        }
        if (Objects.nonNull(queryDto.getFeeStatus())) {
            queryWrapper.eq(DeviceProfitLossDetailPo::getStatus, queryDto.getFeeStatus());
        }
        if (Objects.nonNull(queryDto.getBusinessBillType())) {
            queryWrapper.eq(DeviceProfitLossDetailPo::getBusinessBillType, queryDto.getBusinessBillType());
        }
        if (Objects.nonNull(queryDto.getFeeIncurredTimeStart())) {
            queryWrapper.ge(DeviceProfitLossDetailPo::getFeeIncurredTime, queryDto.getFeeIncurredTimeStart());
        }
        if (Objects.nonNull(queryDto.getFeeIncurredTimeEnd())) {
            queryWrapper.le(DeviceProfitLossDetailPo::getFeeIncurredTime, queryDto.getFeeIncurredTimeEnd());
        }
        return this.baseMapper.selectList(queryWrapper);
    }
}




